﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ControlDesktop
{
    class IniFile
    {
        private string _filePath;
        public IniFile(string filePath)
        {
            _filePath = filePath;
        }

        #region API声明
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        /// <summary>
        /// 写入INI文件信息
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void WriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, _filePath);
        }

        /// <summary>
        /// 读取INI文件信息
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Readvalue(string section, string key)
        {
            StringBuilder strBulider = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", strBulider, strBulider.Capacity, _filePath);
            return strBulider.ToString();
        }
    }
}
